<?php
require_once(SITE_ROOT . 'class/db/DB.class.php');
require_once(SITE_ROOT . 'class/bean/HLeague.class.php');
require_once(SITE_ROOT . 'class/bean/League.class.php');

class DBHLeague {

	public function getHLeague($leagueID) {
		$db = new DB();
		$connection = $db->connect();
		
		if ($stmt = $connection->prepare('SELECT `league#`, `date#`, `bots`, `supporters` FROM `hleague` WHERE `league#` = ?')) {
			$stmt->execute(array($leagueID));
			
			$stmt->bindColumn(1, $leagueID);
			$stmt->bindColumn(2, $date);
			$stmt->bindColumn(3, $bots);
			$stmt->bindColumn(4, $supporters);
			
			$hleague = null;
			
			while ($stmt->fetch()) {
				$league = new League();
				$league->setLeagueID($leagueID);
				
				$hleague = new HLeague($league, strtotime($date));
				$hleague->setNumberBots($bots);
				$hleague->setNumberSupporters($supporters);
			}
			return $hleague;
		}
	}

	
	public function saveHLeagues($hleagues) {
		$db = new DB();
		$connection = $db->connect();
		
		if ($stmt = $connection->prepare('INSERT INTO `hleague` (`league#`, `date#`, `bots`, `supporters`) VALUES (?, ?, ?, ?) ON DUPLICATE KEY UPDATE `bots`=VALUES(`bots`), `supporters`=VALUES(`supporters`)')) {
			foreach ($hleagues as $hleague) {
				$leagueID = $hleague->getLeague()->getLeagueID();
				$date = date("Y-m-d", $hleague->getDate());
				$bots = $hleague->getNumberBots();
				$supporters = $hleague->getNumberSupporters();
				
				$stmt->execute(array($leagueID, $date, $bots, $supporters));
			}
		}
	}
		
}

?>